package org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.http.advanced;


import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelProxyFlowAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.server.AbstractHandleHttpReportClientProxyServerTransferRequestTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.enums.ChannelFlowEnum;
import org.framework.lazy.cloud.network.heartbeat.common.enums.ChannelProtocolType;
import org.framework.lazy.cloud.network.heartbeat.common.enums.ChannelProxyType;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.framework.lazy.cloud.network.heartbeat.server.netty.flow.proxy.ServerChannelProxyFlow;
import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.http.NettyHttpClientProxyServerTransfer;
import org.framework.lazy.cloud.network.heartbeat.server.netty.proxy.http.socket.NettyHttpClientProxyServerRealSocket;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.springframework.stereotype.Component;
import org.wu.framework.spring.utils.SpringContextHolder;

import java.util.ArrayList;
import java.util.List;


/**
 * 服务端处理客户端代理到服务端的请求
 * HTTP_REPORT_CLIENT_PROXY_SERVER_TRANSFER_REQUEST_
 */
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Slf4j
@Component
public class ServerHandleHttpReportClientProxyServerTransferRequestTypeAdvanced
        extends AbstractHandleHttpReportClientProxyServerTransferRequestTypeAdvanced<NettyProxyMsg> {

    private final ChannelProxyFlowAdapter channelProxyFlowAdapter;

    public ServerHandleHttpReportClientProxyServerTransferRequestTypeAdvanced(ChannelProxyFlowAdapter channelProxyFlowAdapter) {
        this.channelProxyFlowAdapter = channelProxyFlowAdapter;
    }

    /**
     * 处理当前数据
     *
     * @param nettyChannelContext 当前通道
     * @param nettyProxyMsg       通道数据
     */
    @Override
    public void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {
        Channel channel = nettyChannelContext.channel();
        // 创建链接、发送数据
        String targetPortString = nettyProxyMsg.targetPort();
        int targetPort = Integer.parseInt(targetPortString);
        String targetIpString = nettyProxyMsg.targetIp();
        String clientIdString = nettyProxyMsg.clientId();
        byte[] data = nettyProxyMsg.getData();
        byte[] visitorId = nettyProxyMsg.getVisitorId();
        String originalIpString = nettyProxyMsg.getOriginalIpString();


        ChannelAttributeKeyUtils.buildClientId(channel, clientIdString);
        ChannelAttributeKeyUtils.buildTargetClientId(channel, clientIdString);
        ChannelAttributeKeyUtils.buildTargetIp(channel, targetIpString);
        ChannelAttributeKeyUtils.buildTargetPort(channel, targetPort);
        ChannelAttributeKeyUtils.buildOriginalIp(channel, originalIpString);

        List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList = new ArrayList<>(SpringContextHolder.getApplicationContext().getBeansOfType(HandleChannelTypeAdvanced.class).values());
        NettyClientProperties nettyClientProperties = SpringContextHolder.getBean(NettyClientProperties.class);
        // 判断代理到客户端还是服务端
        NettyHttpClientProxyServerTransfer nettyHttpClientProxyServerTransfer = new NettyHttpClientProxyServerTransfer();
        nettyHttpClientProxyServerTransfer.setSsl(false);
        nettyHttpClientProxyServerTransfer.setTargetIp(targetIpString);
        nettyHttpClientProxyServerTransfer.setTargetPort(targetPort);


        nettyHttpClientProxyServerTransfer.setHandleChannelTypeAdvancedList(handleChannelTypeAdvancedList);
        nettyHttpClientProxyServerTransfer.setNettyClientProperties(nettyClientProperties);
        nettyHttpClientProxyServerTransfer.setData(data);

        NettyHttpClientProxyServerRealSocket
                .buildRealServer(nettyHttpClientProxyServerTransfer, channel, new String(visitorId));


        // 记录进口数据
        ServerChannelProxyFlow serverChannelFlow = ServerChannelProxyFlow
                .builder()
                .channelFlowEnum(ChannelFlowEnum.IN_FLOW)
                .port(targetPort)
                .ip(targetIpString)
                .clientId(clientIdString)
                .channelProxyType(ChannelProxyType.CLIENT_PROXY_SEVER)
                .channelProtocolType(ChannelProtocolType.HTTP)
                .flow(data.length)
                .build();
        channelProxyFlowAdapter.asyncHandler(channel, serverChannelFlow);
    }

}
